package com.party.core.service.mall.impl;

import com.google.common.base.Strings;
import com.party.common.paging.Page;
import com.party.common.utils.UUIDUtils;
import com.party.core.dao.read.mall.MallCategorySpecReadDao;
import com.party.core.dao.write.mall.MallCategorySpecWriteDao;
import com.party.core.model.mall.MallCategorySpec;
import com.party.core.service.mall.IMallCategorySpecService;
import com.sun.istack.NotNull;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;

/**
 * @Author: Administrator.
 * @Description: 商城分类规格接口实现
 * @Date:Created in 2017/8/22 0022 下午 15:14.
 */
@Service
public class MallCategorySpecService implements IMallCategorySpecService {

    @Autowired
    private MallCategorySpecReadDao mallCategorySpecReadDao;
    @Autowired
    private MallCategorySpecWriteDao mallCategorySpecWriteDao;

    /**
     * 规格关系插入
     *
     * @param mallCategorySpec 规格
     * @return 插入结果
     */
    @Override
    public String insert(MallCategorySpec mallCategorySpec) {
        mallCategorySpec.setId(UUIDUtils.generateRandomUUID());
        boolean result = mallCategorySpecWriteDao.insert(mallCategorySpec);
        if (result) {
            return mallCategorySpec.getId();
        }
        return null;
    }

    /**
     * 规格关系更新
     *
     * @param mallCategorySpec 规格
     * @return 更新结果（true/false）
     */
    @Override
    public boolean update(MallCategorySpec mallCategorySpec) {
        return mallCategorySpecWriteDao.update(mallCategorySpec);
    }

    /**
     * 逻辑删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean deleteLogic(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallCategorySpecWriteDao.deleteLogic(id);
    }

    /**
     * 物理删除
     *
     * @param id 实体主键
     * @return 删除结果（true/false）
     */
    @Override
    public boolean delete(@NotNull String id) {
        if (Strings.isNullOrEmpty(id)) {
            return false;
        }
        return mallCategorySpecWriteDao.delete(id);
    }

    /**
     * 批量逻辑删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDeleteLogic(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallCategorySpecWriteDao.batchDeleteLogic(ids);
    }

    /**
     * 批量删除
     *
     * @param ids 主键集合
     * @return 删除结果（true/false）
     */
    @Override
    public boolean batchDelete(@NotNull Set<String> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        return mallCategorySpecWriteDao.batchDelete(ids);
    }

    /**
     * 获取
     *
     * @param id 主键
     * @return 规格
     */
    @Override
    public MallCategorySpec get(String id) {
        return mallCategorySpecReadDao.get(id);
    }

    /**
     * 分页查询
     *
     * @param mallCategorySpec 规格
     * @param page             分页信息
     * @return 列表
     */
    @Override
    public List<MallCategorySpec> listPage(MallCategorySpec mallCategorySpec, Page page) {
        return mallCategorySpecReadDao.listPage(mallCategorySpec, page);
    }

    /**
     * 查询所有
     *
     * @param mallCategorySpec 规格
     * @return 列表
     */
    @Override
    public List<MallCategorySpec> list(MallCategorySpec mallCategorySpec) {
        return mallCategorySpecReadDao.listPage(mallCategorySpec, null);
    }

    @Override
    public List<MallCategorySpec> batchList(@NotNull Set<String> ids, MallCategorySpec mallCategorySpec, Page page) {
        return null;
    }

    @Override
    public List<MallCategorySpec> findBySpec(String specId) {
        MallCategorySpec categorySpec = new MallCategorySpec("", specId);
        return mallCategorySpecReadDao.listPage(categorySpec, null);
    }

    @Override
    public void deleteByUnique(MallCategorySpec mallCategorySpec) {
        mallCategorySpecWriteDao.deleteByUnique(mallCategorySpec);
    }

    @Override
    public List<MallCategorySpec> findByCategory(String[] categoryIds) {
        return mallCategorySpecReadDao.findByCategory(categoryIds);
    }

    @Override
    public List<MallCategorySpec> findByCategoryId(String categoryId) {
        MallCategorySpec categorySpec = new MallCategorySpec();
        categorySpec.setCategoryId(categoryId);
        return mallCategorySpecReadDao.listPage(categorySpec, null);
    }

    @Override
    public void deleteByCategoryId(String categoryId) {
        mallCategorySpecWriteDao.deleteByCategoryId(categoryId);
    }
}
